package hu.vpszd.services.impl

import hu.vpszd.entities.customer.CheckingAccount
import hu.vpszd.services.CheckingAccountService

import org.apache.commons.lang3.RandomStringUtils
import org.hibernate.Criteria
import org.hibernate.criterion.Restrictions
import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Isolation
import org.springframework.transaction.annotation.Transactional

@Repository("checkingAccountService")
@Transactional(isolation = Isolation.SERIALIZABLE)
class CheckingAccountServiceImpl extends BaseServiceImpl<CheckingAccount> implements CheckingAccountService{

	public List<CheckingAccount> findAll() {
		return findAll(CheckingAccount.class);
	}
	
	public CheckingAccount create(CheckingAccount t) {
		if (t) {
			t.bankAccountNumber = generateAccountNumber()
			return super.create(t);
		}
		return t
	}
	
	private String generateAccountNumber(CheckingAccount t) {
		return RandomStringUtils.randomNumeric(10)
	}
	
	public CheckingAccount findByBankAccountNumber(String bankAccountNumber){
		Criteria criteria = currentSession().createCriteria(CheckingAccount.class);
		CheckingAccount entity = (CheckingAccount) criteria.add(Restrictions.eq("bankAccountNumber", bankAccountNumber)).uniqueResult();
		return entity;
	}
	
}
